package com.le;

import com.le.bean.Piece;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class ChessInit {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate; // 需要配置 Redis数据库

    // 棋子
    private Map<Integer, Piece> PieceInit(){
        Map<Integer, Piece> result = new HashMap<>();
        Piece piece;
        piece = new Piece(1, "車", "红", 1, 0, 0);
        result.put(1, piece);

        piece = new Piece(2, "车", "黑", 1, 0, 9);
        result.put(2, piece);

        piece = new Piece(3, "車", "红", 1, 8, 0);
        result.put(3, piece);

        piece = new Piece(4, "车", "黑", 1, 8, 9);
        result.put(4, piece);

        piece = new Piece(5, "馬", "红", 1, 1, 0);
        result.put(5, piece);

        piece = new Piece(6, "马", "黑", 1, 1, 9);
        result.put(6, piece);

        piece = new Piece(7, "馬", "红", 1, 7, 0);
        result.put(7, piece);

        piece = new Piece(8, "马", "黑", 1, 7, 9);
        result.put(8, piece);

        piece = new Piece(9, "相", "红", 1, 2, 0);
        result.put(9, piece);

        piece = new Piece(10, "象", "黑", 1, 2, 9);
        result.put(10, piece);

        piece = new Piece(11, "相", "红", 1, 6, 0);
        result.put(11, piece);

        piece = new Piece(12, "象", "黑", 1, 6, 9);
        result.put(12, piece);

        piece = new Piece(13, "仕", "红", 1, 3, 0);
        result.put(13, piece);

        piece = new Piece(14, "士", "黑", 1, 3, 9);
        result.put(14, piece);

        piece = new Piece(15, "仕", "红", 1, 5, 0);
        result.put(15, piece);

        piece = new Piece(16, "士", "黑", 1, 5, 9);
        result.put(16, piece);

        piece = new Piece(17, "帅", "红", 1, 4, 0);
        result.put(17, piece);

        piece = new Piece(18, "将", "黑", 1, 4, 9);
        result.put(18, piece);


        piece = new Piece(19, "砲", "红", 1, 1, 2);
        result.put(19, piece);

        piece = new Piece(20, "炮", "黑", 1, 1, 7);
        result.put(20, piece);

        piece = new Piece(21, "砲", "红", 1, 7, 2);
        result.put(21, piece);

        piece = new Piece(22, "炮", "黑", 1, 7, 7);
        result.put(22, piece);


        piece = new Piece(23, "兵", "红", 1, 0, 3);
        result.put(23, piece);

        piece = new Piece(24, "卒", "黑", 1, 0, 6);
        result.put(24, piece);

        piece = new Piece(25, "兵", "红", 1, 2, 3);
        result.put(25, piece);

        piece = new Piece(26, "卒", "黑", 1, 2, 6);
        result.put(26, piece);

        piece = new Piece(27, "兵", "红", 1, 4, 3);
        result.put(27, piece);

        piece = new Piece(28, "卒", "黑", 1, 4, 6);
        result.put(28, piece);

        piece = new Piece(29, "兵", "红", 1, 6, 3);
        result.put(29, piece);

        piece = new Piece(30, "卒", "黑", 1, 6, 6);
        result.put(30, piece);

        piece = new Piece(31, "兵", "红", 1, 8, 3);
        result.put(31, piece);

        piece = new Piece(32, "卒", "黑", 1, 8, 6);
        result.put(32, piece);

        this.redisTemplate.opsForHash().put("chess",  "piece", result);

        return result;
    }

    // 棋盘
    private void ChessBoardInit(Map<Integer, Piece> map){
        int[][] QiPan = new int[9][10];
        // 初始棋盘
        for (Piece p: map.values()){
            QiPan[p.getX()][p.getY()] = p.getId();
        }

        this.redisTemplate.opsForHash().put("board",  1, QiPan);
        this.redisTemplate.opsForHash().put("board",  2, QiPan);

    }

    public static void main(String[] args) {
        ChessInit ci = new ChessInit();
        Map<Integer, Piece> map = ci.PieceInit();
        ci.ChessBoardInit(map);

        Map m = (Map) ci.redisTemplate.opsForHash().get("chess",  "piece");
        System.out.println(m.toString());

        int[][] i = (int[][]) ci.redisTemplate.opsForHash().get("board",  1);
        System.out.println(i);


    }
}
